//
//  SWFeedbackViewController.swift
//  ChildStory
//
//  Created by linhan on 2016/12/22.
//  Copyright © 2016年 Aiya. All rights reserved.
//

import Foundation
import UIKit
class SWFeedbackViewController: SWBaseViewController,UITextViewDelegate,SWAPIComponent
{
    private let MaxCount:Int = 400
    lazy private var _repeater:UITextViewDelegateRepeater = UITextViewDelegateRepeater()
    lazy private var _textView:UITextView = UITextView()
    lazy private var _countLabel:UILabel = UILabel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setup()
    }
    
    override func viewDidDisappear(_ animated: Bool)
    {
        super.viewDidDisappear(animated)
        if navigationController == nil
        {
            SVProgressHUD.dismiss()
        }
    }
    
    private func setup()
    {
        title = "意见反馈"
        automaticallyAdjustsScrollViewInsets = false
        view.backgroundColor = UIColor(hex: 0xf5f5f5)
        
        let font = UIFont.systemFont(ofSize: 14)
        let lineHeight = font.lineHeight
        let numberOfLines:Int = 5
        let inset = _textView.textContainerInset
        let textViewHeight:CGFloat = CGFloat(numberOfLines) * lineHeight + inset.bottom + inset.top
        _textView.font = font
        _textView.frame = CGRectMake(0, navigationBarBottom + 15, view.width, textViewHeight)
        _textView.backgroundColor = UIColor.white
        _textView.textColor = UIColor.black
        view.addSubview(_textView)
        
        let countLabelWidth:CGFloat = 100
        let countLabelHeight:CGFloat = 16
        _countLabel.textColor = UIColor(hex: 0xacacaa)
        _countLabel.font = UIFont.systemFont(ofSize: 12)
        _countLabel.textAlignment = .right
        _countLabel.frame = CGRectMake(_textView.right - countLabelWidth - 13, _textView.bottom - countLabelHeight - 6, countLabelWidth, countLabelHeight)
        view.addSubview(_countLabel)
        
        _repeater.textView = _textView
        _repeater.majorDelegate = self
        
        let maxCharHandler:UITextViewMaxCharHandler = UITextViewMaxCharHandler()
        maxCharHandler.maxChar = MaxCount
        _repeater.addDelegate(maxCharHandler)
        
        let placeholderHandler:UITextViewPlaceholderHandler = UITextViewPlaceholderHandler(textView: _textView)
        placeholderHandler.placeholder = "请简要描述您的问题或意见"
        placeholderHandler.placeholderColor = UIColor(hex:0xacacaa)
        _repeater.addDelegate(placeholderHandler)
        
        
        let labelMargin:CGFloat = 12
        let label:UILabel = UILabel.labelWithLimitWidth(view.width - labelMargin * 2, text: "我们会仔细阅读您的反馈，并尽早给您回复。感谢您的理解与支持。", font: UIFont.systemFont(ofSize: 14))
        label.textColor = UIColor(hex: 0xb3b3b3)
        label.frame = CGRectMake(labelMargin, _textView.bottom + 18, label.width, label.height)
        view.addSubview(label)
        
        let button = createNavigationRightCustomButton("提交", image: nil, target: self, action: #selector(submitBtnTapped))
        button.setTitleColor(UIColor(hex:0x4c4c4c), for: .normal)
        button.setTitleColor(UIColor(hex:0x4c4c4c, alpha:0.5), for: .highlighted)
        
        textViewDidChange(_textView)
    }
    
    private func submit(content:String)
    {
        let req = SWFeedbackReq()
        req.content = content
        req.successCallback = {_,_ in
            SVProgressHUD.showSuccess(withStatus: "已成功提交")
        }
        req.failCallback = {_ in
            SVProgressHUD.showError(withStatus: "提交失败，请稍后重试")
        }
        load(req)
    }
    
    
    func textViewDidChange(_ textView: UITextView)
    {
        let count:Int = (textView.text ?? "").length
        _countLabel.text = "\(count)/\(MaxCount)"
    }
    
    @objc private func submitBtnTapped()
    {
        guard let content = _textView.text, content.length > 0 else{
            return
        }
        SVProgressHUD.show(withStatus: "提交中...")
        submit(content: content)
    }
    
    
    
}
